package com.kwshare.auth.service.impl;

import com.kwshare.auth.service.ILoginService;
import com.kwshare.common.api.RemoteLogService;
import com.kwshare.common.api.RemoteUserService;
import com.kwshare.common.core.domain.Response;
import com.kwshare.common.core.domain.auth.LoginUser;
import com.kwshare.common.core.domain.system.LoginInfo;
import com.kwshare.common.core.utils.IpUtils;
import com.kwshare.common.core.utils.JwtUtils;
import com.kwshare.common.core.utils.SecurityUtils;
import com.kwshare.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 认证中心
 *
 * @author yangjing
 */
@Service
public class LoginServiceImpl implements ILoginService {

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private RemoteLogService remoteLogService;


    /**
     * 登录
     *
     * @param username 账号
     * @param password 密码
     * @return 结果
     */
    @Override
    public LoginUser login(String username, String password, HttpServletRequest httpServletRequest) {
        if(StringUtils.isNull(username)||StringUtils.isNull(password)){
            recordLogininfor(username,"1","账号或密码为空",httpServletRequest);
            throw new NullPointerException("账号或密码为空");
        }
        if("".equals(username)||"".equals(password)){
            recordLogininfor(username,"1","账号或密码为空",httpServletRequest);
            throw new NullPointerException("账号或密码为空");
        }
        Response<LoginUser> userInfo = remoteUserService.userInfo(username);
        if(Response.FAIL == userInfo.getCode()){
            recordLogininfor(username,"1","用户不存在",httpServletRequest);
            throw new RuntimeException("用户不存在");
        }
        if(!password.equals(userInfo.getData().getUser().getPassword())){
            recordLogininfor(username,"1","账号或密码错误",httpServletRequest);
            throw new RuntimeException("账号或密码错误");
        }
        recordLogininfor(username,"0","登录成功",httpServletRequest);
        return userInfo.getData();
    }

    /**
     * 退出登录
     *
     * @param httpServletRequest httpServletRequest
     */
    @Override
    public void logout(HttpServletRequest httpServletRequest) {
        String username = SecurityUtils.getUsername(httpServletRequest);
        recordLogininfor(username,"0","退出登录",httpServletRequest);
    }

    /**
     * 记录登录日志
     *
     * @param username 账号
     * @param status 状态
     * @param message 信息
     * @param httpServletRequest httpServletRequest
     */
    public void recordLogininfor(String username, String status, String message,HttpServletRequest httpServletRequest){
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(username);
        loginInfo.setStatus(status);
        loginInfo.setMessage(message);
        loginInfo.setIpaddr(IpUtils.getIPAddress(httpServletRequest));
        loginInfo.setLoginTime(new Date());
        remoteLogService.add(loginInfo);
    }
}
